package com.weiyuwqj.admin.controller.admin.sys;

import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.service.SysPictureService;
import com.weiyuwqj.api.service.SysArticleService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.model.SysArticle;
import com.weiyuwqj.api.util.IpUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangyong on 2016/7/20.
 */
@Controller
@RequestMapping("/admin/article")
public class ArticleController {

    @Autowired
    private SysArticleService articleService;

    @Autowired
    private SysPictureService pictureService;

    @ResponseBody
    @RequestMapping("/list")
    public Object list(Paginator p, HttpServletRequest request) {
        String name = request.getParameter("name");
        String siteId = request.getParameter("siteId");
        String status = request.getParameter("status");
        String beginDateTime = request.getParameter("beginDateTime");
        String endDateTime = request.getParameter("endDateTime");
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(siteId)) {
            map.put("siteId", siteId);
        }
        if (StringUtils.isNotEmpty(name)) {
            map.put("name", name);
        }
        if (StringUtils.isNotEmpty(status)) {
            map.put("status", status);
        }
        if (StringUtils.isNotEmpty(beginDateTime)) {
            map.put("beginDateTime", beginDateTime);
        }
        if (StringUtils.isNotEmpty(endDateTime)) {
            map.put("endDateTime", endDateTime);
        }
        return articleService.getList(p, map);
    }


    @ResponseBody
    @RequestMapping("/modify")
    public void modify(SysArticle article, String pics, @RequestParam(defaultValue = "0") Integer hasImage) {
        if (hasImage.equals(0)) {
            //只修改文章的信息
            articleService.updateNotNull(article);
        } else if (hasImage.equals(1)) {
            //删除对应文章的缩略图
            int ItemId = article.getId();
            Map map = new HashMap();
            map.put("itemId", ItemId);
            map.put("pictureType", 5);
            pictureService.deleteByItemId(map);
            //插入一条对应文章的缩略图
//            sysArticle = contentService.selectOne(sysArticle);
            pictureService.addArticlePic(pics, article);
            articleService.updateNotNull(article);
        }
    }

    @ResponseBody
    @RequestMapping("/add")
    public void add(SysArticle article, String pics) {
        article.setAddip(IpUtils.get());
        article.setAddtime(new Date());
        article.setHits(0);
        article.setUpdateuserId(0);
        articleService.insertArticle(article, pics);
    }

    @ResponseBody
    @RequestMapping(value = "/admin/article/getSysArticleById", method = RequestMethod.POST)
    public Object getSysArticleById(Integer id) {
        return articleService.selectByKey(id);
    }


    /**
     * 删除
     * @param articleId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteByArticleId", method = RequestMethod.POST)
    public Object deleteByArticleId(Integer articleId) {
         articleService.deleteById(articleId);
        return Message.OK;
    }
}
